export const formatDate = function unixtimetostr1(unixtime) {
    var dateTime = new Date(parseInt(unixtime) * 1000);
    var year = dateTime.getFullYear();
    var month = dateTime.getMonth() + 1;
    var day = dateTime.getDate();
    var timeSpanStr = year + '-' + month + '-' + day;
    return timeSpanStr;
}

export function unixtimetostr(unixtime) {
    var dateTime = new Date(parseInt(unixtime) * 1000);
    var year = dateTime.getFullYear();
    var month = dateTime.getMonth() + 1;
    var day = dateTime.getDate();
    var timeSpanStr = year + '-' + month + '-' + day;
    return timeSpanStr;
}

export function unixtimetostr1(unixtime) {
    var dateTime = new Date(unixtime);
    var year = dateTime.getFullYear();
    var month = dateTime.getMonth() + 1;
    var day = dateTime.getDate();
    var timeSpanStr = year + '-' + month + '-' + day;
    return timeSpanStr;
}

export function dateformat(datestr) {
    var dateTime = new Date(datestr);
    var year = dateTime.getFullYear();
    var month = dateTime.getMonth() + 1;
    var day = dateTime.getDate();
    var timeSpanStr = year + '-' + month + '-' + day;
    return timeSpanStr;
}

//unix纪元时间，毫秒
export function unixtimestamp() {
    return new Date().getTime();
}

//unix时间，秒
export function unixtime() {
    let timestamp = new Date().getTime();
    return Math.floor(timestamp / 1000);
}

export function todaystr() {
    var dateTime = new Date();
    var year = dateTime.getFullYear();
    var month = dateTime.getMonth() + 1;
    var day = dateTime.getDate();
    var timeSpanStr = year + '-' + month + '-' + day;
    return timeSpanStr;
}

//生成0到1之间的随机数
export function random0To1() {
    return Math.random();
}

//生成指定范围[n, m)的随机数
export function randomInRange(n, m) {
    return Math.random() * (m - n) + n;
}

//生成0到n-1之间的随机整数: let randomInt = Math.floor(Math.random() * n); console.log(randomInt); 
export function randomInt(n) {
    return Math.floor(Math.random() * n);
}

//生成1到n之间的随机整数: let randomIntOneToN = Math.floor(Math.random() * n) + 1; console.log(randomIntOneToN);
export function randomInt1ToN(n) {
    return Math.floor(Math.random() * n) + 1;
}

//生成指定位数的纯数字字符串: function randomNum(n) { 
export function randomNumStr(n) {
    let res = "";
    for (let i = 0; i < n; i++) {
        res += Math.floor(Math.random() * 10);
    } return res;
}

//生成指定位数的数字字母混合字符串: 
export function generateMixed(n) {
    let chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
    let res = "";
    for (let i = 0; i < n; i++) {
        let id = Math.floor(Math.random() * 36);
        res += chars[id];
    }
    return res;
}

export default {
    formatDate, unixtimetostr, unixtimetostr1, dateformat, unixtimestamp, unixtime, todaystr,
    random0To1, randomInRange, randomInt, randomInt1ToN, randomNumStr, generateMixed
}
